
/**
 * Date:2019年5月31日下午4:27:38
 * Copyright (c) 2019, www.bwbroad.com All Rights Reserved.
 *
*/

package com.xue.im.common.coder;

import java.io.IOException;

import org.jboss.marshalling.ByteOutput;

import io.netty.buffer.ByteBuf;

/**
 * Description: TODO <br/>
 * Date: 2019年5月31日 下午4:27:38 <br/>
 * 
 * @author xuejianxin
 */
public class MyChannelBufferByteOutput implements ByteOutput {

	private final ByteBuf buffer;

	/**
	 * Create a new instance which use the given {@link ByteBuf}
	 */
	MyChannelBufferByteOutput(ByteBuf buffer) {
		this.buffer = buffer;
	}

	@Override
	public void close() throws IOException {
		// Nothing to do
	}

	@Override
	public void flush() throws IOException {
		// nothing to do
	}

	@Override
	public void write(int b) throws IOException {
		buffer.writeByte(b);
	}

	@Override
	public void write(byte[] bytes) throws IOException {
		buffer.writeBytes(bytes);
	}

	@Override
	public void write(byte[] bytes, int srcIndex, int length) throws IOException {
		buffer.writeBytes(bytes, srcIndex, length);
	}

	/**
	 * Return the {@link ByteBuf} which contains the written content
	 *
	 */
	ByteBuf getBuffer() {
		return buffer;
	}
}